package cn.ciis.sys.setting;

import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.model.Setting;
import cn.ciis.sys.permission.Notes;
import cn.ciis.sys.permission.Remark;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.ehcache.CacheKit;

/** @author 冉志林 */
public class SettingAdminController extends BaseController {
  @Inject
  SettingAdminService srv;

  @Notes(value = "系统设置管理")
  @Remark(value = "系统设置界面")
  public void index() {
    Setting maxId = (Setting) srv.findMaxId();
    if (null != maxId) {
      set("model", maxId);
    }
    kIndex();
  }

  @Remark(value = "系统设置保存更新")
  public void saveOrUpdate() {
    Setting model = getModel(Setting.class);
    Model<?> maxId = srv.findMaxId();
    if (null != maxId) {
      model.setId(maxId.getInt("id"));
    }
    Ret ret = srv.saveOrUpdate(model);
    if (ret.isOk()) {
      CacheKit.removeAll("setting");
      CacheKit.put("setting", "setting2019", srv.findMaxId());
      ret.set("setting", srv.findMaxId());
    }
    renderJson(ret);
  }
}
